package com.sgb.kjwl.view.ui.adapter.self.website;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.self.WebsitePhotoEntity;

import java.util.ArrayList;
import java.util.List;


public class WebsitePhotoEditAdapter3 extends RecyclerView.Adapter<WebsitePhotoEditAdapter3.EditViewHolder> {

    private Context mContext;
    private List<WebsitePhotoEntity> urls;
    private List<WebsitePhotoEntity> selectedUrls = new ArrayList<>();
    private SelectedListener selectedListener;

    public WebsitePhotoEditAdapter3(Context context,List<WebsitePhotoEntity> list){
        this.mContext = context;
        this.urls = list;
    }

    @NonNull
    @Override
    public EditViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_website_photo3, viewGroup, false);
        return new EditViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull EditViewHolder viewHolder, int position) {
        Glide.with(mContext).load(urls.get(position).getUrl()).into(viewHolder.img);
        viewHolder.delView.setVisibility(View.VISIBLE);

        if(selectedUrls.contains(urls.get(position))){
            viewHolder.delView.setBackgroundResource(R.mipmap.selected_website);
        }else{
            viewHolder.delView.setBackgroundResource(R.mipmap.unselected_website);
        }

        viewHolder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(selectedUrls.contains(urls.get(position))){
                    selectedUrls.remove(urls.get(position));
                }else{
                    selectedUrls.add(urls.get(position));
                }
                notifyDataSetChanged();
                if(null!=selectedListener){
                    selectedListener.setData(selectedUrls);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return urls == null ? 0 : urls.size();
    }

    /**
     * 正常内容的viewholder
     */
    class EditViewHolder extends RecyclerView.ViewHolder {
        ImageView img;
        ImageView delView;

        EditViewHolder(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.photo_img);
            delView = itemView.findViewById(R.id.select_iv);
        }
    }

    public interface SelectedListener{
        void setData(List<WebsitePhotoEntity> urls);
    }

    public void setSelectListener(SelectedListener listener){
        this.selectedListener = listener;
    }

    public void setSelectAll(boolean flag){
        if(flag){
            selectedUrls.clear();
            selectedUrls.addAll(urls);
        }else{
            selectedUrls.clear();
        }
        notifyDataSetChanged();
    }

    public List<WebsitePhotoEntity> getSelectedUrls(){
        return selectedUrls;
    }
}